/*
** main.c for asm in /home/vanet_c//proj-kw/asm
** 
** Made by charles-edouard vanet
** Login   <vanet_c@epitech.net>
** 
** Started on  Thu Dec  6 12:27:04 2012 charles-edouard vanet
** Last update Sat Dec 15 19:41:50 2012 charles-edouard vanet
*/

#include	"../includes/asm.h"

int	make_read(char *file, t_list **file_list, int my_ac, int index)
{
  int	file_ok;
  int	fd;
  char	*tmp_file;

  tmp_file = epur_file_name(my_strdup(file));
  file_ok = check_file(file);
  if (file_ok == 1)
    return (1);
  else if (file_ok == 2)
    {
      my_error("", file, " Expecting an assembly file.\n", 0);
      return (2);
    }
  if ((fd = open(file, O_RDONLY)) == -1)
    my_error("", file, " Can not open this file.\n", 1);
  if (index > 1)
    my_putchar('\n');
  my_putstr("\e[1mAssembling ");
  my_putstr(tmp_file);
  free(tmp_file);
  my_putstr(" in progress...\e[0m\n");
  check_instruction(file, file_list, my_ac, fd);
  return (0);
}

int	get_my_ac(char **av)
{
  int	i;
  int	res;

  i = 0;
  res = 0;
  while (av[++i])
    {
      if (check_file(av[i]) == 0)
	res++;
    }
  return (res);
}

int		main(int ac, char **av)
{
  int		i;
  t_list	*file_list;
  t_list	*tmp;
  int		my_ac;

  i = 0;
  if (ac >= 2)
    {
      my_ac = get_my_ac(av);
      file_list = my_param_in_list(ac, av);
      tmp = file_list;
      while (av[++i] && tmp != NULL)
	{
	  if (make_read(av[i], &file_list, my_ac, i) == 1)
	    my_error("File ", av[i], " not accessible.\n", 0);
	  tmp = tmp->next;
	  my_free_one_tlist(file_list);
	  file_list = tmp;
	}
      if (file_list != NULL)
	my_free_list(file_list);
    }
  else
    usage_asm();
  return (0);
}
